#include "admindialog.h"
#include "ui_admindialog.h"

AdminDialog::AdminDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AdminDialog)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    //ui->listWidget->setDragEnabled(false);
    //ui->listWidget->setWordWrap(true);
    //ui->listWidget->setWrapping(false);
    ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
    ui->listWidget->setFrameShape(QListWidget::NoFrame);
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    ui->listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->listWidget->setFocusPolicy(Qt::NoFocus);
    ui->listWidget->setViewMode(QListWidget::ListMode);
    ui->listWidget->setGridSize(QSize(590, 80));
    //ui->listWidget->setFlow(QListWidget::TopToBottom);
    QScroller::grabGesture(ui->listWidget, QScroller::TouchGesture);
    //connect(ui->listWidget, &QListWidget::itemClicked, this, &User::itemClicked);


    ui->comboBox->setView(new QListView());
    QString data = "QComboBox QAbstractItemView::item {"
                "height: 80px;}" ;  /* 项的高度（设置pComboBox->setView(new QListView());后，该项才起作用） */
    ui->comboBox->setStyleSheet(data);
    ui->comboBox->setCurrentIndex(0);

    QList<Book_t> list = Sqlite::Get()->Select_book();
    foreach(Book_t book, list)
    {
        listWidgetAddItem(book.name, book.num);
    }
}

AdminDialog::~AdminDialog()
{
    delete ui;
}

void AdminDialog::on_comboBox_currentIndexChanged(int index)
{
    qDebug() << "index : " << index;
    ui->listWidget->clear();
    if(index == 0)
    {
        QList<Book_t> list = Sqlite::Get()->Select_book();
        foreach(Book_t book, list)
        {
            listWidgetAddItem(book.name, book.num);
        }
    }
    else
    {
        QList<Lend_t> list = Sqlite::Get()->Select_lend();
        foreach(Lend_t book, list)
        {
            //qDebug() <<
            listWidgetAddItem(book.book, book.user, book.lend);
        }
    }
}

void AdminDialog::listWidgetAddItem(QString book, int num)
{
    QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
    item->setData(NameRole, book);
    //item->setData(SellRole, sell);
    item->setData(NumRole, num);
    item->setSizeHint(QSize(590, 80));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget();
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(QString("《%1》").arg(book), widget);
    icon_label->setFixedSize(460, 80);
    icon_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    icon_label->setStyleSheet("color:#000;font-size:30px;");
    icon_label->move(0,0);

    QLabel *label = new QLabel(QString("还剩%1本").arg(num), widget);
    label->setFixedSize(130, 80);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("color:#000;font-size:30px;");
    label->move(460,0);

    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item, widget);
}

void AdminDialog::listWidgetAddItem(QString book, QString user, QString time)
{
    QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
    item->setData(NameRole, book);
    //item->setData(SellRole, sell);
    //item->setData(NumRole, num);
    item->setSizeHint(QSize(590, 80));
    QSize size = item->sizeHint();

    QWidget *widget = new QWidget();
    widget->setFixedSize(size);

    QLabel *icon_label = new QLabel(QString("《%1》").arg(book), widget);
    icon_label->setFixedSize(460, 50);
    icon_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    icon_label->setStyleSheet("color:#000;font-size:30px;");
    icon_label->move(0,0);

    QDateTime time2 = QDateTime::fromTime_t(time.toUInt());
    qDebug() << time2; // QDateTime(2022-02-23 09:03:41.000 HKT Qt::LocalTime)
    QString data = time2.toString(" yyyy-MM-dd hh:mm:ss");

    QLabel *user_label = new QLabel(QString("出借时间:%1").arg(data), widget);
    user_label->setFixedSize(460, 30);
    user_label->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    user_label->setStyleSheet("color:#000;font-size:20px;");
    user_label->move(0,50);

    QLabel *label = new QLabel(QString("出借人：%1").arg(user), widget);
    label->setFixedSize(190, 80);
    label->setAlignment(Qt::AlignCenter);
    label->setStyleSheet("color:#000;font-size:20px;");
    label->move(400,0);

    ui->listWidget->addItem(item);
    ui->listWidget->setItemWidget(item, widget);
}

void AdminDialog::on_pushButton_clicked()
{
    this->hide();
}
